package com.pap.product.product.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.exception.IdValidationException;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.product.agent.product.IProductAgent;
import com.pap.product.agent.product.IProductDetailAgent;
import com.pap.product.agent.product.dto.ProductDTO;
import com.pap.product.agent.product.dto.ProductSelectedDetailsDTO;
import com.pap.product.product.auto.entity.Product;

@RestController
@RequestMapping("/v1/product/product")
public class ProductController extends BaseController<Product, IProductAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(ProductController.class);
	
	@Resource(name = "productAgent")
	@Override
	public void setService(IProductAgent agent) {
		super.service = agent;
	}
	
	@Autowired
	private IProductDetailAgent productDetailAgent;
	
	/**
	 * 插入或者更新
	 * @param clientLicenseId
	 * @param loginUserId	
	 * @param saveupdateJSON
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveupdate")
	public ResponseVO<Product> saveOrUpdate(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "saveupdateJSON", required = true) String saveupdateJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		ProductDTO e = objectMapper.readValue(saveupdateJSON, ProductDTO.class);

		int operationInt = service.saveOrUpdateProductWithDetail(e, clientLicenseId, loginUserId);
		return ResponseVO.successdata(operationInt);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delete")
	public ResponseVO<Product> delete(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "deleteJSON", required = true) String deleteJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		Product e = objectMapper.readValue(deleteJSON, Product.class);
		
		if (null == e.getId()) {
			// id不存在
			throw new IdValidationException("删除操作，不存在此项数据!");
		}

		int operationInt = service.deleteProductWithDetail(e.getId());
		return ResponseVO.successdata(operationInt);
	}
	
	/**
	 * 查询产品定义明细数据，分为两部分，一部分是单值对应的itemId,另一部分为字典值对应的dictValue
	 * @param clientLicenseId
	 * @param loginUserId
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/selectdetailsitembyitemvaluetype/{productId}")
	public ResponseVO<ProductSelectedDetailsDTO> selectDetailsItemByItemValueType(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String productId) throws Exception {
		List<ProductSelectedDetailsDTO> list = productDetailAgent.selectCheckedProductItems(productId);

		return ResponseVO.successdatas(list, null);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/selectproductinfo/{productId}")
	public ResponseVO<Product> selectProductInfo(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String productId) throws Exception {
		Product product = service.selectByPrimaryKey(productId);
		List<ProductSelectedDetailsDTO> list = productDetailAgent.selectCheckedProductItems(productId);

		
		return ResponseVO.successdatawithdatas(product, list, null);
	}
}